//
//  PackageCollectionViewCell.m
//  UI_HLJ_Frame
//
//  Created by dllo on 15/7/1.
//  Copyright (c) 2015年 毕宏伟. All rights reserved.
//

#import "PackageCollectionViewCell.h"
#import "PackageCell.h"
#import "CaseCell.h"
#import "MerchantCell.h"

#import "PackageModel.h"
#import "CaseModel.h"
#import "NewMerchantModel.h"

#import "UIImageView+WebCache.h"
// 跳转到套餐详情
#import "PackageDetailsViewController.h"

#define WIDTH self.contentView.frame.size.width
#define HEIGHT self.contentView.frame.size.height
@implementation PackageCollectionViewCell



- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        self.packageTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT ) style:UITableViewStylePlain];
        self.packageTableView.delegate = self;
        self.packageTableView.dataSource = self;
        [self.contentView addSubview:self.packageTableView];
        [self.packageTableView release];
        
        //不显示cell间分割线
//        self.packageTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        

        
        self.packageArr = [NSMutableArray array];
        self.caseArr = [NSMutableArray array];
        self.merchantArr = [NSMutableArray array];
    }
    return self;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (self.flag == 0) {
        return self.packageArr.count;
    }else if (self.flag == 1){
        return self.caseArr.count;
    }else if (self.flag == 2){
        return self.merchantArr.count;
    }else{
        return 0;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.flag == 0) {
        static NSString *reuse = @"reuse";
        PackageCell *packagecell = [tableView dequeueReusableCellWithIdentifier:reuse];
        
//        // 动画效果
//        packagecell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
//        [UIView animateWithDuration:0.5 animations:^{
//        packagecell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
//        }];
        packagecell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        if (!packagecell) {
        packagecell = [[PackageCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
        }
        

        PackageModel *packageModel = self.packageArr[indexPath.row];
        
//        [packagecell.packageImageView sd_setImageWithURL:[NSURL URLWithString:packageModel.coverpath]];
        [packagecell.packageImageView sd_setImageWithURL:[NSURL URLWithString:packageModel.coverpath] placeholderImage:[UIImage imageNamed:@"占位图"]];
        packagecell.packageTitle.text = packageModel.title;
        packagecell.packageTitle.font = [UIFont systemFontOfSize:17];
        
        packagecell.packageName.text = packageModel.merchant[@"name"];
        packagecell.packageName.font = [UIFont systemFontOfSize:14];
        

        CGFloat actual_price1 = [packageModel.actual_price floatValue];
        NSString *actual_price = [NSString stringWithFormat:@"现价￥ %.0f", actual_price1];
        packagecell.packageNowPrice.text = actual_price;
        packagecell.packageNowPrice.font = [UIFont systemFontOfSize:17];
        
        CGFloat market_price1 = [packageModel.market_price floatValue];
        NSString *market_price = [NSString stringWithFormat:@"市场价￥ %.0f", market_price1];
        packagecell.packageMarketPrice.text = market_price;
        packagecell.packageMarketPrice.font = [UIFont systemFontOfSize:14];
        
        CGFloat collectors_count1 = [packageModel.collectors_count floatValue];
        NSString *collectors_count = [NSString stringWithFormat:@"💍 %.0f", collectors_count1];
        packagecell.packageCollect.text = collectors_count;
        packagecell.packageCollect.font = [UIFont systemFontOfSize:14];
        
        return packagecell;
    }else if (self.flag == 1){
        static NSString *reuse = @"caseReuse";
        CaseCell *casecell = [tableView dequeueReusableCellWithIdentifier:reuse];
        
//        // 动画效果
//        casecell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
//        [UIView animateWithDuration:0.5 animations:^{
//            
//            casecell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
//        }];
        
        casecell.selectionStyle = UITableViewCellSelectionStyleNone;
        
       
        if (!casecell) {
            casecell = [[CaseCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
        }
        CaseModel *caseModel = self.caseArr[indexPath.row];
        [casecell.caseImageView sd_setImageWithURL:[NSURL URLWithString:caseModel.coverpath]];
        casecell.caseTitle.text = caseModel.title;
        casecell.caseName.text = caseModel.merchant[@"name"];
        CGFloat collectors_count1 = [caseModel.collectors_count floatValue];
        NSString *collectors_count = [NSString stringWithFormat:@"💍 %.0f", collectors_count1];
        casecell.caseCollect.text = collectors_count;
        casecell.caseCollect.font = [UIFont systemFontOfSize:14];
        
        return casecell;
    }else{
        static NSString *reuse = @"merchantReuse";
        MerchantCell *merchantcell = [tableView dequeueReusableCellWithIdentifier:reuse];
//        // 动画效果
//        merchantcell.layer.transform = CATransform3DMakeScale(0.3, 0.3, 0.1);
//        [UIView animateWithDuration:0.5 animations:^{
//            
//        merchantcell.layer.transform = CATransform3DMakeScale(1, 1, 0.1);
//        }];
        
        merchantcell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        
        if (!merchantcell) {
            merchantcell = [[MerchantCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuse];
        }

        NewMerchantModel *NewMerchantModel = self.merchantArr[indexPath.row];
        [merchantcell.merchantImageView sd_setImageWithURL:[NSURL URLWithString:NewMerchantModel.logopath]];
        merchantcell.merchantName.text = NewMerchantModel.name;
        CGFloat active_works_pcount1 = [NewMerchantModel.active_works_pcount floatValue];
        NSString *active_works_pcount = [NSString stringWithFormat:@"套餐 %.0f", active_works_pcount1];
        merchantcell.merchanrPackage.text = active_works_pcount;
        
        CGFloat active_cases_pcount1 = [NewMerchantModel.active_cases_pcount floatValue];
        NSString *active_cases_pcount = [NSString stringWithFormat:@"案例 %0.f", active_cases_pcount1];
        merchantcell.merchanrCase.text = active_cases_pcount;
        
        CGFloat fans_count1 = [NewMerchantModel.fans_count floatValue];
        NSString *fans_count = [NSString stringWithFormat:@"粉丝 %0.f", fans_count1];
        merchantcell.merchanrFans.text = fans_count;
        
        return merchantcell;
    }

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.flag == 0) {
        return (HEIGHT-64-49) / 2;
    }else if (self.flag == 1){
        return (HEIGHT-64-49) / 2;
    }else{
        return (HEIGHT-64-49) / 6;
    }
}

- (void)setPackageArr:(NSMutableArray *)packageArr
{
    if (_packageArr != packageArr) {
        _packageArr = [packageArr retain];
        [_packageArr release];
        
    }

    [self.packageTableView reloadData];
}

- (void)setCaseArr:(NSMutableArray *)caseArr
{
    if (_caseArr != caseArr) {
        _caseArr = [caseArr retain];
        [_caseArr release];
    }
    [self.packageTableView reloadData];
}
- (void)setMerchantArr:(NSMutableArray *)merchantArr
{
    if (_merchantArr != merchantArr) {
        _merchantArr = [merchantArr retain];
        [_merchantArr release];
    }
    [self.packageTableView reloadData];
}

@end
